package o;

import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.gamebox.plugin.gameservice.bean.CheckSignResp;
import com.huawei.gamebox.plugin.gameservice.bean.GameInfo;
import com.huawei.gamebox.plugin.gameservice.db.tables.GameSignRecord;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.List;
import org.json.JSONObject;

public class ezt {
    private static ezt ˊ;
    private int ʻ = 0;
    private long ʼ;
    private boolean ʽ;
    private String ˋ;
    private GameInfo ˎ;
    private String ˏ;
    private String ॱ;
    private long ॱॱ;
    private fad ᐝ;

    static class e implements IServerCallBack {
        private e() {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }
    }

    public static synchronized ezt ˏ() {
        ezt o_ezt;
        synchronized (ezt.class) {
            if (ˊ == null) {
                ˊ = new ezt();
            }
            o_ezt = ˊ;
        }
        return o_ezt;
    }

    public void ˋ(GameInfo gameInfo, String str, String str2, String str3, fad o_fad) {
        this.ˎ = gameInfo;
        this.ᐝ = o_fad;
        this.ˏ = str;
        if (str2 == null) {
            str2 = "";
        }
        this.ˋ = str2;
        this.ॱ = str3;
        this.ʽ = false;
    }

    public void ॱ() {
        if (this.ˎ == null || this.ᐝ == null || this.ˏ == null || this.ॱ == null) {
            Logger.e("GameSignManager", "the params is null when call check the sign");
        } else if (!ezw.ॱ().ˊ()) {
            Logger.ˋ("GameSignManager", "protocol not checked, the checkSign not run");
        } else if (this.ʽ) {
            Logger.ˋ("GameSignManager", "the checkSign has checked before");
        } else {
            this.ʽ = true;
            this.ʻ = 0;
            this.ʼ = System.currentTimeMillis();
            if (ˋ()) {
                this.ॱॱ = System.currentTimeMillis();
                ˋ(0);
                return;
            }
            Logger.ˋ("GameSignManager", "check the sign from local failed");
            ˏ(this.ˏ, this.ˋ, this.ॱ);
        }
    }

    private boolean ˋ() {
        List ˏ = ezi.ˏ().ˏ(this.ˎ.getPackageName(), this.ˎ.getAppId());
        if (ˏ == null || ˏ.size() <= 0) {
            if (Logger.ˎ()) {
                Logger.ˏ("GameSignManager", "query game key from db is null:" + this.ˎ.getPackageName() + "," + this.ˎ.getAppId());
            }
            return false;
        }
        GameSignRecord gameSignRecord = (GameSignRecord) ˏ.get(0);
        if (!ॱ(gameSignRecord)) {
            Logger.ˋ("GameSignManager", "check game sign failed");
            return false;
        } else if (ˊ(gameSignRecord)) {
            return true;
        } else {
            Logger.ˋ("GameSignManager", "check game apk sign failed");
            return false;
        }
    }

    private boolean ॱ(GameSignRecord gameSignRecord) {
        boolean z = false;
        try {
            z = ˊ((this.ˎ.getAppId() + this.ˎ.getCpId() + this.ॱ).getBytes(StandardCharsets.UTF_8), gameSignRecord.ˊ(), this.ˏ);
        } catch (Exception e) {
            Logger.e("GameSignManager", "check game sign exception");
        }
        return z;
    }

    private boolean ˊ(GameSignRecord gameSignRecord) {
        String ॱ = gameSignRecord.ॱ();
        String ʻ = gameSignRecord.ʻ();
        if (dof.ˋ(ॱ) || dof.ˋ(ʻ)) {
            return false;
        }
        try {
            ॱ = evh.ˊ().ॱ(ॱ, ʻ);
            if (ॱ == null) {
                return false;
            }
            ʻ = ",";
            for (String equalsIgnoreCase : ॱ.split(",")) {
                if (equalsIgnoreCase.equalsIgnoreCase(this.ˋ)) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            Logger.e("GameSignManager", "AES decrypt failed");
            return false;
        }
    }

    private void ˏ(String str, String str2, String str3) {
        this.ʻ = 1;
        new fac("", this.ᐝ).ˎ(this.ˎ, str, str2, str3, new e(this) {
            final /* synthetic */ ezt ˏ;

            {
                this.ˏ = r2;
            }

            public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
                this.ˏ.ॱॱ = System.currentTimeMillis();
                if (!(responseBean == null || responseBean.getResponseCode() == 0)) {
                    switch (responseBean.getResponseCode()) {
                        case 2:
                            ezk.ˋ(null, 110016);
                            break;
                        case 3:
                            ezk.ˋ(null, 110017);
                            break;
                        default:
                            ezk.ˋ(null, 110018);
                            break;
                    }
                    this.ˏ.ˋ(1);
                }
                if ((responseBean instanceof CheckSignResp) && (o_cvu instanceof eyp) && responseBean.getResponseCode() == 0) {
                    CheckSignResp checkSignResp = (CheckSignResp) responseBean;
                    if (checkSignResp.getRtnCode_() == 0 && !checkSignResp.ˎ()) {
                        Logger.e("GameSignManager", "check sign on server failed, for checked error");
                        ezk.ˋ(null, 110019);
                        this.ˏ.ˋ(1);
                        return;
                    } else if (this.ˏ.ॱ(checkSignResp)) {
                        this.ˏ.ˋ(0);
                        if (!dof.ॱ(checkSignResp.ॱ())) {
                            if (Logger.ˎ()) {
                                Logger.ˏ("GameSignManager", "start to save the game key on db");
                            }
                            GameSignRecord gameSignRecord = new GameSignRecord();
                            gameSignRecord.ˊ(this.ˏ.ˎ.getPackageName());
                            gameSignRecord.ˎ(this.ˏ.ˎ.getAppId());
                            String ˏ = checkSignResp.ˏ();
                            String ˏ2 = dnk.ˏ(evh.ˋ());
                            gameSignRecord.ˏ(evh.ˊ().ˎ(ˏ, ˏ2));
                            gameSignRecord.ॱ(ˏ2);
                            gameSignRecord.ˋ(checkSignResp.ॱ());
                            ezi.ˏ().ॱ(gameSignRecord);
                            return;
                        }
                        return;
                    }
                }
                this.ˏ.ˋ(1);
            }
        });
    }

    private boolean ॱ(CheckSignResp checkSignResp) {
        return checkSignResp.getRtnCode_() == 0 && checkSignResp.ˎ();
    }

    private boolean ˊ(byte[] bArr, String str, String str2) throws Exception {
        if (dof.ॱ(str) || dof.ॱ(str2)) {
            Logger.e("GameSignManager", "the publicKey or sign is null");
            return false;
        }
        PublicKey generatePublic = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(dnk.ˎ(str)));
        Signature instance = Signature.getInstance("SHA256WithRSA");
        instance.initVerify(generatePublic);
        instance.update(bArr);
        return instance.verify(dnk.ˎ(str2));
    }

    private void ˋ(int i) {
        try {
            if (Logger.ˎ()) {
                Logger.ˏ("GameSignManager", "handle check sign result:" + i);
            }
            ˊ();
            ezw.ॱ().ˊ(i);
        } catch (Exception e) {
            Logger.e("GameSignManager", "notify the check game sign result to sdk exception");
        }
    }

    private void ˊ() {
        if (this.ᐝ != null) {
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("type", this.ʻ);
                jSONObject.put("time", this.ॱॱ - this.ʼ);
                this.ᐝ.ˋ("checkSign", jSONObject.toString());
            } catch (Throwable e) {
                Logger.ˋ("GameSignManager", "handle check sign result exception", e);
            }
        }
    }
}
